//
//  YWReadCell.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/15.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWReadCell.h"
#import <UIView+SDAutoLayout.h>
#import <UIImageView+WebCache.h>

@implementation YWReadCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setUpViews];
    }
    return self;
}

-(void)setUpViews
{
    _bgView                       = [UIView new];
    _bgView.layer.masksToBounds   = YES;
    _bgView.layer.borderColor     = [UIColor grayColor].CGColor;
    _bgView.layer.borderWidth     = 1;
    _bgView.layer.cornerRadius    = 10;

    _bgImage                      = [UIImageView new];
    _topicLabel                   = [UILabel new];
    _topicLabel.font              = [UIFont systemFontOfSize:12];
    _titleLabel                   = [UILabel new];
    _titleLabel.numberOfLines     = 2;
    _titleLabel.font              = [UIFont systemFontOfSize:15];
    _lineLabel                    = [UILabel new];
    _lineLabel.backgroundColor    = [UIColor lightGrayColor];
    _sourceLabel                  = [UILabel new];
    _sourceLabel.font             = [UIFont systemFontOfSize:12];
    _deleteButton                 = [UIButton new];
    [_deleteButton setBackgroundImage:[UIImage imageNamed:@"subs_delete"] forState:UIControlStateNormal];
    [_deleteButton addTarget:self action:@selector(unLikeBtn) forControlEvents:UIControlEventTouchUpInside];

    [self.contentView addSubview:_bgView];
    [self.contentView addSubview:_bgImage];
    [self.contentView addSubview:_topicLabel];
    [self.contentView addSubview:_titleLabel];
    [self.contentView addSubview:_lineLabel];
    [self.contentView addSubview:_sourceLabel];
    [self.contentView addSubview:_deleteButton];

    
    _bgView.sd_layout
    .spaceToSuperView(UIEdgeInsetsMake(10, 10, 0, 10));
    
    _bgImage.sd_layout
    .topSpaceToView(self.contentView,15)
    .leftSpaceToView(self.contentView,15)
    .widthIs(150)
    .autoHeightRatio(0.65);
    
    _topicLabel.sd_layout
    .topSpaceToView(self.contentView,15)
    .leftSpaceToView(_bgImage,10)
    .autoHeightRatio(0);
    
    _titleLabel.sd_layout
    .topSpaceToView(_topicLabel,5)
    .leftEqualToView(_topicLabel)
    .rightSpaceToView(self.contentView,10)
    .maxHeightIs(80)
    .autoHeightRatio(0);
    
    _lineLabel.sd_layout
    .topSpaceToView(_bgImage,10)
    .leftEqualToView(_bgImage)
    .rightEqualToView(_titleLabel)
    .heightIs(1);
    
    _sourceLabel.sd_layout
    .topSpaceToView(_lineLabel,5)
    .leftEqualToView(_lineLabel)
    .widthIs(100)
    .autoHeightRatio(0);
    
    
    _deleteButton.sd_layout
    .topEqualToView(_sourceLabel)
    .heightRatioToView(_sourceLabel,1)
    .widthEqualToHeight()
    .rightSpaceToView(self.contentView,15);
    [self setupAutoHeightWithBottomViewsArray:@[_bgImage,_topicLabel,_titleLabel,_sourceLabel] bottomMargin:10];
}


-(void)unLikeBtn
{
    if (self.deleteBlock)
    {
        _deleteBlock(_model.unlikeReason);
    }
}


#pragma mark - set -
-(void)setModel:(YWReadInternalBaseClass1 *)model
{
    _model            = model;
    _topicLabel.text  = _model.source;
    _sourceLabel.text = _model.source;
    _titleLabel.text  = _model.title;
    [_bgImage sd_setImageWithURL:[NSURL URLWithString:_model.imgsrc]];
    
}
@end
